package tech.aistar.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import tech.aistar.config.AlipayConfig;

import java.io.UnsupportedEncodingException;

public class FlowController {

    @PostMapping("alipay")
    @ResponseBody
    public String alipay(Integer proId, String proName, String orderId, String orderNum, Float orderPrice) throws AlipayApiException {

        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id,
                AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
        //创建API对应的request
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
//        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        //在公共参数中设置回跳和通知地址
        alipayRequest.setNotifyUrl("");
        String timeout_express = "1c";
        String body = "";
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + orderNum + "\","
                + "\"total_amount\":\"" + orderPrice + "\","
                + "\"subject\":\"" + proName + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();
        System.out.println("执行支付请求后，result:" + result);
        return result;
    }

    // 同步请求 支付宝调用接受后，数据响应，展示流水信息
//    @GetMapping("alipayReturnNotice")
//    public ModelAndView alipayReturnNotice(HttpServletRequest request) throws UnsupportedEncodingException, AlipayApiException {
//        System.out.println("同步请求，支付成功后，支付宝的返回数据是：" + request);
//
//        //商户订单号
//        String orderNum = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");
//        //支付宝交易号
//        String tradeNo = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");
//        //付款金额
//        String payPrice = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8");
//
//        System.out.println("return orderNum:" + orderNum);
//        System.out.println("return tradeNo:" + tradeNo);
//        System.out.println("return payPrice:" + payPrice);
//
//        //
//
//        return ;
    }

//    // 异步请求回调
//    @PostMapping("alipayNotifyNotice")
//    @ResponseBody
//    public String alipayNotifyNotice(HttpServletRequest request) throws AlipayApiException, UnsupportedEncodingException {
//        System.out.println("异步请求，支付成功后，支付宝的返回数据是：" + request);
//        //商户订单号
//        String orderNum = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");
//        //支付宝交易号
//        String tradeNo = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");
//        //付款金额
//        String payPrice = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8");
//
//        System.out.println("return orderNum:" + orderNum);
//        System.out.println("return tradeNo:" + tradeNo);
//        System.out.println("return payPrice:" + payPrice);
//
//        //
//
//
//    }


